package cn.lrbj.grpc.server.provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.grpc.service.UserProviderGrpc.UserProviderImplBase;
import cn.grpc.service.UserProviderOuterClass.UserIdRequest;
import cn.grpc.service.UserProviderOuterClass.UserVoReplay;
import cn.grpc.service.UserProviderOuterClass.UserVoReplay.Builder;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * Grpc服务暴露
 *
 * @author qinjian
 * @datetime 2019/11/24 14:36
 */
@GrpcService
public class UserProvider extends UserProviderImplBase {
    // 定义日志输出
    private final Logger logger = LoggerFactory.getLogger(UserProvider.class);

    @Override
    public void getByUserId(UserIdRequest request, StreamObserver<UserVoReplay> responseObserver) {
        // 获取请求数据
        long userId = request.getId();
        logger.info("grpc request: userId=" + userId);

        Builder userVoReplayBuild = UserVoReplay.newBuilder();
        userVoReplayBuild.setId(userId);
        userVoReplayBuild.setUsername("hello world");

        // 做出响应
        responseObserver.onNext(userVoReplayBuild.build());
        responseObserver.onCompleted();
    }
}